package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.Constants;
import com.recsmile.sgs.core.Player;
import com.recsmile.sgs.entity.ClientRoom;
import io.netty.channel.Channel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

/**
 * Created by Administrator on 2016/8/26.
 */
public class RoomGui extends JPanel{

    private static class PlayerUI extends JPanel{
        private JLabel nameLabel;
        private JLabel locationLabel;
        //private Image image;

        PlayerUI(int location, Player info) {
            //setPreferredSize(new Dimension(200, 200));
            JPanel top = new JPanel();
            top.setLayout(new GridLayout(1,2,10,10));

            if (info != null) {
                nameLabel = new JLabel(info.getNickname(), null,SwingConstants.CENTER);
                locationLabel = new JLabel("Location " + location, null,SwingConstants.CENTER);
            } else {
                nameLabel = new JLabel("Empty", null,SwingConstants.CENTER);
                locationLabel = new JLabel("Empty", null,SwingConstants.CENTER);
            }
            top.add(nameLabel);
            top.add(locationLabel);
            setLayout(new BorderLayout());
           //final Image image = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("cards/card_back.png" )).getImage();
//            JPanel comp = new JPanel(){
//                @Override
//                protected void paintComponent(Graphics g) {
//                    super.paintComponent(g);
//                    g.drawImage(image,0,0,image.getWidth(this),image.getHeight(this), this);
//                }
//            };
            add(top,BorderLayout.NORTH);
            add(new JLabel(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("cards/card_back.png" ))),BorderLayout.CENTER);
        }

        void updateUser(Player user) {
            nameLabel.setText(user.getNickname());
        }

        void clearLabel() {
            nameLabel.setText("");
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.gray);
            g.fillRect(0,0,getWidth(),getHeight());
        }
    }
    private static final int HGAP = Constants.SCREEN_WIDTH / 160;
    private static final int VGAP = Constants.SCREEN_HEIGHT / 90;

    private List<PlayerUI> playerUIs;
    private Channel channel;

    public RoomGui(ClientRoom room, Channel channel) {
        this.channel = channel;
        this.playerUIs = new ArrayList<>();
        //this.setPreferredSize(new Dimension(Constants.SCREEN_WIDTH/2,Constants.SCREEN_HEIGHT/2));
        JPanel ctrlButtopn = new JPanel();
        ctrlButtopn.add(new ControlButtonGui("Leave", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //channel.writeAndFlush();
            }
        }));
        ctrlButtopn.add(new ControlButtonGui("Start", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //channel.writeAndFlush();
            }
        }));


        JPanel playerJPanel = new JPanel(new GridLayout(2,4,HGAP,VGAP));
        List<Player> players = new ArrayList<>(room.getPlayers().values());
        for(int i =0 ;i < room.getCapacity();i++){
            PlayerUI ui = new PlayerUI(i, i >= players.size() ? null : players.get(i));
            playerJPanel.add(ui);
            playerUIs.add(ui);
        }

        setLayout(new BorderLayout());
        add(playerJPanel, BorderLayout.CENTER);
        add(ctrlButtopn,BorderLayout.SOUTH);
    }
}
